/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'banners',
                condition: {type: 1},
                model: {
                    url: _global.url.api + 'banner/{id}',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove'
                    },
                    edit: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            content: template('add-tpl', this.data),
                            width: 500,
                            ok: function () {
                                _this.data['type'] = 1;
                                model.data = _this.data;
                                model.put();
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        $('input,select,textarea').on('change', function (e) {
                            _this.change(e);
                        });
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                model.del();
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    content: template('add-tpl', {}),
                    width: 500,
                    ok: function () {
                        _this.data['type'] = 1;
                        if (new Model({
                                url: _global.url.api + 'banner',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
                $('input,select,textarea').on('change', function (e) {
                    _this.change(e);
                });
            });

            //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});